package com.sky.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dao.SetmealDTO;
import com.sky.dao.SetmealPageDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.entity.SetmealDishes;
import com.sky.exception.CanDeleteException;
import com.sky.exception.NoFoundException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealDishVO;
import com.sky.vo.SetmealPageVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;


    public PageResult page(SetmealPageDTO setmealPageDTO) {
        PageHelper.startPage(setmealPageDTO.getPage(), setmealPageDTO.getPageSize());
        Page<SetmealPageVO> page = setmealMapper.page(setmealPageDTO);
        if (page == null || page.size() <= 0) {
            throw new NoFoundException(MessageConstant.PACKAGE_NOT_FOUND);
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.save(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDishes> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDishes setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        String categoryName = categoryMapper.getNameById(setmeal.getCategoryId());
        ArrayList<SetmealDishes> setmealDishes = setmealDishMapper.getBySetmealId(id);


        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setCategoryName(categoryName);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;


    }

    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        for (SetmealDishes setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        ArrayList<SetmealDishes> setmealDishes = setmealDishMapper.getBySetmealId(id);
        for (SetmealDishes setmealDish : setmealDishes) {
            Long dishId = setmealDish.getDishId();
            Dish dish = dishMapper.getById(dishId);
            Integer dishStatus = dish.getStatus();
            if (dishStatus == StatusConstant.DISABLE) {
                throw new CanDeleteException(MessageConstant.CAN_NOT_DELETE_DISABLE_DISH);
            }
        }
        setmealMapper.update(setmeal);
    }


    @Transactional
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            Integer status = setmeal.getStatus();
            if (status == StatusConstant.ENABLE) {
                throw new CanDeleteException(MessageConstant.CAN_NOT_DELETE_ENABLE_SETMEAL);
            }
            setmealDishMapper.deleteBySetmealId(id);
            setmealMapper.delete(id);
        }
    }

    @Override
    public List<Setmeal> list(Long categoryId) {

        return setmealMapper.list(categoryId);
    }

    @Override
    public List<SetmealDishVO> getBySetmealId(Long id) {
        List<SetmealDishVO> list= setmealDishMapper.getSetmealDishVO(id);
        return list;
    }
}
